package com.csc.util;

import com.csc.exception.LogicException;
import java.io.*;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.List;
import java.util.Vector;

/**
 * @description:
 * @author: ChenShuCheng
 * @create: 2021-06-04 20:57
 **/
public class FileUtil {


    /**
     * 顺序写
     * @param filePath
     * @param content
     * @param index
     * @return
     */
    public static long fileWrite(String filePath,String content,int index){

        File file = new File(filePath);
        try {
            RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
            FileChannel fileChannel = randomAccessFile.getChannel();
            MappedByteBuffer map = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, 1024 * 1024);
            map.position(index);
            map.put(content.getBytes());
            return map.position();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0L;
    }


    /**
     * 顺序读
     * @param filePath
     * @param index
     * @return
     */
    public static String fileRead(String filePath, long index) {
        File file = new File(filePath);
        RandomAccessFile randomAccessTargetFile;
        MappedByteBuffer map;
        try {
            randomAccessTargetFile = new RandomAccessFile(file, "rw");
            FileChannel targetFileChannel = randomAccessTargetFile.getChannel();
            map = targetFileChannel.map(FileChannel.MapMode.READ_WRITE, 0, index);
            byte[] byteArr = new byte[10 * 1024];
            map.get(byteArr, 0, (int) index);
            return new String(byteArr);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
        return "";
    }


    /**
     * 文件合并
     * @param inputPaths
     * @param outputPath
     * @throws FileNotFoundException
     */
    public static void mergeFile(List<String> inputPaths, String outputPath) throws FileNotFoundException {

        Vector<InputStream> inputStream = new Vector<InputStream>();

        if (inputPaths == null || inputPaths.isEmpty()) {
            throw new LogicException("合并文件路径不能为空");
        }

        for (String inputPath : inputPaths) {
            InputStream in = new  FileInputStream(new File(inputPath));
            inputStream.add(in);
        }

        //构造一个合并流
        SequenceInputStream stream = new SequenceInputStream(inputStream.elements());
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(
                    new FileOutputStream(outputPath));

            byte[] bytes = new byte[10240];
            int len = -1;
            while((len=stream.read(bytes))!=-1){
                bos.write(bytes,0,len);
                bos.flush();
            }
            System.out.println("文件合并完成!");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (null != bos ) {
                    bos.close();
                }
                stream.close();
            } catch (IOException ignored) {
            }
        }
    }
}
